# VEXcode mkrules.mk 2019_03_26_01

# compile C files
$(BUILD)/%.o: %.c $(SRC_H)
	$(Q)$(MKDIR)
	$(ECHO) "CC  $<"
	$(Q)$(CC) $(CFLAGS) $(INC) -c -o $@ $<
	
# compile C++ files
$(BUILD)/%.o: %.cpp $(SRC_H) $(SRC_A)
	$(Q)$(MKDIR)
	$(ECHO) "CXX $<"
	$(Q)$(CXX) $(CXX_FLAGS) $(INC) -c -o $@ $<
	
# create executable 
$(BUILD)/$(PROJECT).elf: $(OBJ)
	$(ECHO) "LINK $@"
	$(Q)$(LINK) $(LNK_FLAGS) -o $@ $^ $(LIBS)
	$(Q)$(SIZE) $@ 

# create binary 
$(BUILD)/$(PROJECT).bin: $(BUILD)/$(PROJECT).elf
	$(Q)$(OBJCOPY) -O binary $(BUILD)/$(PROJECT).elf $(BUILD)/$(PROJECT).bin

# create archive
$(BUILD)/$(PROJECTLIB).a: $(OBJ)
	$(Q)$(ARCH) $(ARCH_FLAGS) $@ $^

# clean project
clean:
	$(info clean project)
	$(Q)$(CLEAN)
###### 使用方法： 复制下面所有代码（或者本文所有内容）， 通过“记事本”或者vexcode打开：你的程序项目/vex/mkrules.mk，并将代码粘贴到mkrules.mk最后面
###### instructions: copy all the code below（or the whole text of this text file）, with "notepad.exe" or vexcode, open the file: your-project-folder/vex/mkrules.mk, then paste  the code to the end of mkrules.mk 
###########################################################################################################
### 生成自动依赖关系的文件，当头文件修改时，可以自动重新编译关联的cpp和c文件
### Generates an automatic dependency .d file, 
### which automatically recompiles the associated .cpp and .c file when the header .h file is modified!
### 以下是windows CMD代, linux 好像不一样
### the code works in windows CMD, maybe not in linux

### 命令包1: 生成 .d 文件的命令包，供cpp和c文件使用。如果头文件不存在，则只会生成空文件
### command pack 1: generate .d file for compinling c\cpp files
define gen_dep_with_headers_check_except_trailing_space
$(Q)$(MKDIR)
@$(ECHO) -------------making dependency file^: $@ , added by LQ from VEX team 74000
@$(CXX) $(INC) -MM -MT "$@ $(subst .d,.o,$@)"  $< >$@ 2>nul
endef

### 命令包2: 判断vexcode要做的事情：清除 或者 编译
### 如果传递的指令是clean等,则不执行；编译时，MAKECMDGOALS为空，则执行gen_dep来生成d文件
### command pack 2: if make clean, it's not going to generate .d files;  
### if compiling project(only "make" called,thus $(MAKECMDGOALS) is empty),  $(gen_dep) will run to generate .d files
define conditional_generate_dependency_files
$(if  $(MAKECMDGOALS), , $(gen_dep_with_headers_check_except_trailing_space))
endef

### c\cpp文件的d文件静态生成模式
### Static Pattern Rule for .d file from cpp and c file
$(BUILD)/%.d: %.cpp	$(SRC_H) $(SRC_A)
	$(conditional_generate_dependency_files)
$(BUILD)/%.d: %.c $(SRC_H) $(SRC_A)
	$(conditional_generate_dependency_files)

### 引入所有的.d依赖文件
### include all .d files for auto recompiling
-include $(OBJ:.o=.d)
###########################################################################################################

###### 注意 1：粘贴后，如果指令($(conditional_generate_dependency_files) )前的tab不小心被vexcode替换成了两个空格，则会出现类似报错：vex/mkrules.mk:64: *** multiple target patterns.  Stop.  
###### 解决办法：重新粘贴，不要在vexcode中编辑。
###### Note 1: After pasting, if the tab in front of the command($(conditional_generate_dependency_files) ) is replaced by Vexcode with two spaces, a similar error occurs: Vex mkrules.mk : 64: * * * multiple target patterns. Stop.
###### Solution: just re-paste, save , and do not edit mkrules.mk in vexcode

###### 注意 2：在cpp/c 的源代码中，#include指令的<> 或者"" 中的头文件名字后面，不要加入空格，例如：#include "my_header.h   "。否则d文件能成功生成，但是会报错。
###### 解决办法：删除头文件名后的多余空格，然后关闭vexcode,重新双击你的项目文件(xxx.v5code),软件就会自动清除编译的缓存； 或者你自己手动删除项目文件夹中的build文件夹。
###### Note 2: in the CPP/C source code, do not add spaces after the name of the header file in the #include directive, for example: # include "my_header.h   ". Otherwise, the .d file can be generated successfully, 
###### but an error will be reported. 
###### Solution: remove the extra space after the header file name, close vexcode, and double-click Your Project File (xxx.v5code) , the software automatically clears the compiled cache; or you can manually delete the build folder from the project folder.
